//
//  XMMainViewController.swift
//  XMWeiCreatProject
//
//  Created by XM on 16/8/8.
//  Copyright © 2016年 XM. All rights reserved.
//

import UIKit

class XMMainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.tintColor = UIColor.orangeColor()
        
        //通过获取的json中的字符串 来设置根控制器
        setUpChilds()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //添加➕按钮
        setUpComposeBtn()
    }

    func composeBtnClick() {
        print(#function)
    }
    
    private func setUpComposeBtn() {
        tabBar.addSubview(compose_btn)
        let btn_width = UIScreen.mainScreen().bounds.size.width / CGFloat(childViewControllers.count)
        let rect = CGRectMake(0, 0, btn_width, 49)
        compose_btn.frame = CGRectOffset(rect, 2 * btn_width, 0)
    }
    
    private func setUpChilds() {
        let json = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        if let jsonPath = json {
            
            let jData = NSData(contentsOfFile: jsonPath)
            do {
                let jsonArr = try NSJSONSerialization.JSONObjectWithData(jData!, options: .MutableContainers)
                for dict in jsonArr as! [[String:String]] {
                    addChildViewController(dict["vcName"]!, title_name: dict["title"]!, image_name: dict["imageName"]!)
                }
                
            } catch {
                addChildViewController("XMHomeController", title_name: "首页", image_name: "tabbar_home")
                addChildViewController("XMMessageController", title_name: "信息", image_name: "tabbar_message_center")
                //添加一个空的控制器 用来设置“➕”按钮
                addChildViewController("XMNullViewController", title_name: "", image_name: "")
                addChildViewController("XMDiscoverController", title_name: "发现", image_name: "tabbar_discover")
                addChildViewController("XMMeController", title_name: "我的", image_name: "tabbar_profile")
            }
            
        }
    }
    
    private func addChildViewController(childController_name: String, title_name: String, image_name: String) {
        
        // 1.“字符串”转换成“类的名字”  2.创建类的“对象”  3.命名空间.类的字符串名称
        //print(childController) <XMWeiCreatProject.XMHomeController: 0x7fb66bd0e6d0>
        
        let mingming_name = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        let classes = NSClassFromString(mingming_name + "." + childController_name) as! UIViewController.Type
        let childController = classes.init()
        
        childController.title = title_name
        childController.tabBarItem.image = UIImage(named: image_name)
        childController.tabBarItem.selectedImage = UIImage(named: image_name + "_highlighted")
        
        let nav = UINavigationController()
        nav.addChildViewController(childController)
        addChildViewController(nav)
        
        
    }
    
    private lazy var compose_btn: UIButton = {
        var btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: #selector(XMMainViewController.composeBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
}
